const https = require("../../utils/https");

// pages/inspection_input/input.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    //上传凭证
    selectImageList: [],
    picarr: [],
    picarrShow: [],
    isShow: true,
    content: '',
    check_point_result_id: 0,
    loaiding: false,
    disabled: false,
    act: 'add',
    id: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this,
      is_login_inspector = wx.getStorageSync('is_login_inspector');
    if (!is_login_inspector || is_login_inspector != 1) {
      wx.showToast({
        title: '请先登陆验房师账号',
        icon: 'none',
        duration: 1500
      })
      wx.navigateBack({
        delta: 0,
      })
      return false;
    }
    if (options && options.check_point_result_id != '') {
      that.setData({
        check_point_result_id: options.check_point_result_id,
        id: options.id?options.id:0
      })
    }
    if(options.id && options.id > 0){
      that.setData({
        act: 'edit'
      })
      that.getQuestionInfo();
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
  getQuestionInfo: function(){
    var that = this,
        id = this.data.id;
    https.post('getQuestionInfo',{id:id},false,'加载中').then(res=>{
      if(res.picarr.length >= 6){
        var is_show = false;
      }else{
        var is_show = true;
      }
      that.setData({
        content: res.content,
        picarr: res.picarr,
        picarrShow: res.picarrShow,
        is_show: is_show
      })
    })
  },
  /**
   * @xk 文字变换
   * @param {*} obj 
   */
  chanageContent: function (obj) {
    this.setData({
      content: obj.detail.value
    })
  },
  /**
   * @xk 准备提交
   */
  submit: function () {
    var that = this,
      content = this.data.content,
      id = this.data.id,
      act = this.data.act,
      picarr = this.data.picarr,
      newList = this.data.selectImageList;
      console.log(newList.length);
    if (content == '') {
      wx.showToast({
        title: '请描述验房问题',
        icon: 'none',
        duration: 2000
      })
      return false;
    } else if (newList.length > 0) {
      that.setData({
        loaiding: true,
        disabled: true,
        content: content,
        pics: picarr.join(',')
      })
      that.uploadPic({
        path: newList
      });
    }else{
      that.setData({
        loaiding: true,
        disabled: true,
        content: content,
        pics: picarr.join(',')
      })
      that.formsubimt();
    }
  },
  uploadPic: function (data) {
    var pics = this.data.pics ? this.data.pics : '';
    var that = this;
    var i = data.i ? data.i : 0; //当前上传的哪张图片
    var success = data.success ? data.success : 0; //上传成功的个数
    var fail = data.fail ? data.fail : 0; //上传失败的个数
    var photo_urls = this.data.photo_urls ? this.data.photo_urls : [];
    console.log(https.url);
    wx.uploadFile({
      url: https.url + 'upload',
      filePath: data.path[i],
      name: 'file', //这里根据自己的实际情况改
      formData: {
        id: data.id
      }, //这里是上传图片时一起上传的数据
      success: (resp) => {
        if (resp.data != '') {
          success++; //图片上传成功，图片上传成功的变量+1
          wx.showLoading({
            title: '上传第' + success + '张完成',
          })
          if (pics != '') {
            pics = pics + ',' + resp.data
          } else {
            pics = resp.data
          }
        } else {
          fail++
        }
        that.setData({
          pics: pics,
          imgs: [],
        })
        //这里可能有BUG，失败也会执行这里,所以这里应该是后台返回过来的状态码为成功时，这里的success才+1
      },
      fail: (res) => {
        console.log(res);
        fail++; //图片上传失败，图片上传失败的变量+1
      },
      complete: () => {
        i++; //这个图片执行完上传后，开始上传下一张
        if (i == data.path.length) { //当图片传完时，停止调用          
          wx.hideLoading();
          wx.showToast({
            title: '上传完成',
            icon: 'success',
            duration: 1000
          });
          setTimeout(function () {
            that.formsubimt();
          }, 1000);

        } else { //若图片还没有传完，则继续调用函数
          data.i = i;
          data.success = success;
          data.fail = fail;
          that.uploadPic(data);
        }
      }
    });
  },
  /**
   * @xk 提交保存问题
   */
  formsubimt: function () {
    var that = this,
      content = this.data.content,
      act = this.data.act,
      id = this.data.id,
      check_point_result_id = this.data.check_point_result_id,
      pics = this.data.pics;
    https.post('saveCheckQuestion', {
      content: content,
      pics: pics,
      act: act,
      id: id,
      check_point_result_id: check_point_result_id
    }, false, '正在提交问题').then(res => {
      wx.navigateBack({
        delta: 0,
      })
    })
  },

  //点击上传图片
  selectBottom(e) {
    var that = this;
    const selectNum = that.data.selectImageList.length;
    const picarrNum = this.data.picarr.length;
    const num = 6 - (selectNum+picarrNum);
    num != 0 && wx.chooseImage({
      count: num,
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: (res) => {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        let tempFilePaths = res.tempFilePaths;
        let newList = that.data.selectImageList;
        for (let i = 0; i < tempFilePaths.length; i++) {
          newList.push(tempFilePaths[i]);
        }
        (newList.length+picarrNum) == 6 ? that.setData({
          isShow: false
        }) : null;
        that.setData({
          selectImageList: newList
        }, () => {})
      }
    })
  },
  //删除
  closeOption(e) {
    const {
      index
    } = e.currentTarget.dataset;
    let imagelist = this.data.selectImageList;
    imagelist.splice(index, 1);
    this.setData({
      selectImageList: imagelist,
      isShow: true
    })
  },
  closeOptionx(e) {
    const {
      index
    } = e.currentTarget.dataset;
    let imagelist = this.data.picarr;
    let picarrshow = this.data.picarrShow;
    imagelist.splice(index, 1);
    picarrshow.splice(index,1);
    this.setData({
      picarr: imagelist,
      picarrShow: picarrshow,
      isShow: true
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },
})